<?php

App::uses('Crud', 'Model');

class Question extends Crud
{
    /**
     * Al Agregar/eliminar/actualizar las constantes de los modelos implica ir
     * y actualizar los comentarios en los campos de la tabla asociada a este modelo.
     */
    const STATUS_ENABLED = 1;
    const STATUS_DISABLED = 0;
    const SELECTABLE = 1;
    const MANDATORY = 1;
    const MANDATORY_OFF = 0;
    const REQUIRED_ON = 1;
    const REQUIRED_OFF = 0;

    /**
     * Constantes para el tipo de source_id
     */
    const MOE_TYPE = 1;
    const AREA_TYPE = 2;
    const LINE_TYPE = 3;
    const WORKSTATION_TYPE = 4;

    /**
     * Obtenemos los registros para el grid del admin
     * @return array
     */
    public function getEnabledBySource($params)
    {
        $filters = array(
            'fields' => array(
                'description as text',
                'id as value',
                'image as img',
                'video as vid',
                'selectable as selec',
                'mandatory',
                'type as src_type',
                'required',
                'admissible_answers'
            ),
            'order' => array(
                'id ASC'
            ),
        );
        $filters['conditions'] = array(
            'status' => self::STATUS_ENABLED,
            'OR' => array(
                'source_id' => $params
            )
        );
        $records = $this->find('all', $filters);
        return $this->flatArray($records);
    }

    /**
     * Regresa las preguntas obligatorias u opcionales para la WS
     */
    public function getForExam($hierarchy, $maxQuestions, $required=TRUE) 
    {    
        $filters = array(
                    'conditions' => array(
                            'required' => $required,
                            'selectable'=> self::SELECTABLE,
                            'status' => self::STATUS_ENABLED,
                            'source_id' => array($hierarchy['ws_id'],
                                                 $hierarchy['line_id'],
                                                 $hierarchy['area_id'],
                                                 $hierarchy['moe_id']) ) );
        $resRecords = $this->find('all', $filters);
        $records = $this->flatArray($resRecords);
        shuffle($records);
        if( !$required )
        {
            $records = array_slice($records, 0, $maxQuestions);
        }
        return $records;
    }

}